package com.mobiusws.reports.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.mobiusws.reports.action.DailyFraudDetailAction;
import com.mobiusws.reports.action.DailyFraudSummaryAction;
import com.mobiusws.reports.model.CallType;
import com.mobiusws.reports.model.DailyFraudDetail;
import com.mobiusws.reports.model.DailyFraudSummary;
import com.mobiusws.reports.model.LoginInfo;
import com.mobiusws.util.JsonUtil;
import com.mobiusws.util.SqlUtil;
import com.mysql.jdbc.PreparedStatement;

@Repository("dailyFraudDetailDao")
public class DailyFraudDetailDao {
	@Resource
	private JdbcTemplate dbxJdbc;
	@Resource
	private JdbcTemplate coreJdbc;
	@Resource
	private SqlUtil sqlUtil;
	private Logger logger = Logger.getRootLogger();

	private int cnt;
	private List<?> list;

	private void logStmt(String sql, Object[] param) {
		logger.info(sql);
		StringBuilder logTxtBld = new StringBuilder();
		for (Object para : param) {
			logTxtBld.append(para).append(", ");
		}
		logger.info(logTxtBld.substring(0, logTxtBld.length() - 2));
	}

	private void execQuery(JdbcTemplate jdbc, String sql, Object[] param,
			BeanPropertyRowMapper rowMapper, QueryType queryType) {
		logStmt(sql, param);
		switch (queryType) {
		case INT:
			this.cnt = jdbc.queryForInt(sql, param);
			break;
		case LIST:
			this.list = jdbc.query(sql, param, rowMapper);
			break;
		}
	}

	public List<DailyFraudDetail> getDailyFraudDetailByMSISDN(
			DailyFraudDetailAction action) {
		if(action.getStart()==0 || action.getTotal()<1) {
			String sqlCnt = sqlUtil.getSqlById("getFraudDetailByMsisdnCount");
			Object[] param = new Object[] { action.getSelDate(), action.getMsisdn() };
			execQuery(dbxJdbc, sqlCnt, param, null, QueryType.INT);
			action.setTotal(cnt);
		}
		if (action.getTotal() > 0) {
			String sql = sqlUtil.getSqlById("getFraudDetailByMsisdn");
			Object[] params = new Object[] { action.getSelDate(),
					action.getMsisdn(), action.getStart(), action.getLimit() };
			BeanPropertyRowMapper rowMapper = new BeanPropertyRowMapper(
					DailyFraudDetail.class);
			execQuery(dbxJdbc, sql, params, rowMapper, QueryType.LIST);
		} else {
			this.list = new ArrayList();
		}
		return (List<DailyFraudDetail>) this.list;
	}
	
	public List<List<Object>> getQueryList(DailyFraudDetailAction action) {
		List<List<Object>> ls = new ArrayList<List<Object>>();
		try {
			Connection conn = dbxJdbc.getDataSource().getConnection();
			ResultSet rs = null;
			List<Object> lsl = null;
			String sql = sqlUtil.getSqlById("getFraudDetailByMsisdnExport");
			java.sql.PreparedStatement stmt = null;
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, action.getSelDate());
			stmt.setLong(2, action.getMsisdn());
			rs = stmt.executeQuery();
			int column = rs.getMetaData().getColumnCount();
			while(rs.next()) {
				lsl = new ArrayList<Object>();
				for(int i=0; i < column; i++) {
					lsl.add(rs.getString(i+1));
				}
				ls.add(lsl);
			}
			
			stmt = null;
			rs = null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		return ls;
	}
}
